//index.js  
//获取应用实例  
var app = getApp(), t = app.requirejs("core"), a = app.requirejs("wxParse/wxParse"), i = app.requirejs("biz/diypage");
Page({
  data: {
    /** 
        * 页面配置 
        */
    winWidth: 0, 
    winHeight: 0,
    // tab切换  
    currentTab: 0,

    // 产品假数据
    listshop: [
      { 'time': '05/19/2018', 'type': '待付款', 'img': 'http://www.xinmoo.cn/uploadfile/graphy/large/20150306100857m821gawc.jpg?location=Lcenter', 'name': 'skincare护肤品', 'num': '1', 'money': '598', 'bind1': 'tofxdddetail', 'bind2': '######', 'bind3': '######' },
      { 'time': '05/19/2018', 'type': '已付款', 'img': 'http://www.xinmoo.cn/uploadfile/graphy/large/20150306100857m821gawc.jpg?location=Lcenter', 'name': 'skincare精华', 'num': '1', 'money': '98', 'bind1': 'tofxdddetail' ,'bind2': '######', 'bind3': '######' },
      { 'time': '05/19/2018', 'type': '已完成', 'img': 'http://www.xinmoo.cn/uploadfile/graphy/large/20150306100857m821gawc.jpg?location=Lcenter', 'name': 'skincare洗面乳', 'num': '1', 'money': '199', 'bind1': 'tofxdddetail', 'bind2': '######', 'bind3': '######' },
      { 'time': '05/19/2018', 'type': '已取消', 'img': 'http://www.xinmoo.cn/uploadfile/graphy/large/20150306100857m821gawc.jpg?location=Lcenter', 'name': 'skincare护肤品', 'num': '1', 'money': '998', 'bind1': 'tofxdddetail', 'bind2': '######', 'bind3': '######' },
      { 'time': '05/19/2018', 'type': '已完成', 'img': 'http://www.xinmoo.cn/uploadfile/graphy/large/20150306100857m821gawc.jpg?location=Lcenter', 'name': 'skincare面霜', 'num': '1', 'money': '698', 'bind1': 'tofxdddetail', 'bind2': '######', 'bind3': '######' },
    ],
  },
  
  onLoad: function () {
    var that = this;

    /** 
     * 获取系统信息 
     */
    wx.getSystemInfo({

      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }

    });
    this.getAgentOrder();
  },
  getAgentOrder:function(filter=false){
    var that = this;
    t.get("member/index/getAgentOrder", {}, function (e) {
      var res = e.filter(item=>{
        if(filter===false){
          return true
        }else
          return item.status == filter;
      })
      res.map(item => {
        let createTime = new Date(item.createtime * 1000);
        item.createtime = createTime.getFullYear() + "-" + createTime.getMonth() + "-" + createTime.getDate();
        switch (Number(item.status)) {
          case 0:
            item.statusMsg = "待付款";
            break;
          case 1:
            item.statusMsg = "待发货";
            break;
          case 2:
            item.statusMsg = "已付款";
            break;
          case 3:
            item.statusMsg = "已完成";
            break;
          case -1:
            item.statusMsg = "已关闭";
            break;
          default:
            item.statusMsg = "未知状态";
            break;
        }
      })
      that.setData({ list: res })
    });
  },
  tofxdddetail: function (e) {
    if (e.currentTarget.dataset.status==3){
      wx.navigateTo({
        url: '/pages/daili/fxdd/detail?order_id=' + e.currentTarget.dataset.id,
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    }    
  },
  /** 
     * 滑动切换tab 
     */
  bindChange: function (e) {
    this.getAgentOrder();
    var that = this;
    that.setData({ currentTab: e.detail.current });

  },
  /** 
   * 点击tab切换 
   */
  swichNav: function (e) {

    var that = this;

    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      var filter=false;
      switch (Number(e.target.dataset.current)) {
        case 0:
          filter = false;
          break;
        case 1:
          filter = 0;
          break;
        case 2:
          filter = 2;
          break;
        case 3:
          filter = 3;
          break;
        default:
          filter = false;
          break;
      }
      this.getAgentOrder(filter);
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  }
})  